package com.qingluan.widget;

import com.qingluan.Database.Personal_Info_database;
import com.qingluan.dd.PersonInfoActivity;
import com.qingluan.dd.R;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;

public class SelectPictureActivity extends Activity {
	private final int TakephotoCode = 2;
	private final int ChoosePhotoCode =1 ;
	private LinearLayout linearLayout ;
	Intent intentCallback ;
	//context argu area
	private Context context_this_activity;
	private String tag = this.getClass().getName();
	//info argu area 
	private String uid;
	
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_picture);
		context_this_activity = getApplicationContext();
		
		uid = getIntent().getStringExtra("uid");
		linearLayout = (LinearLayout) findViewById(R.id.activity_select_bc);
		linearLayout.setBackgroundDrawable(getPicture());
		intentCallback = getIntent(); 
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.select_picture, menu);
		return true;
	}
	
	private Drawable getPicture(){
		String pic_utl = new Personal_Info_database(context_this_activity).searchBitmap(uid);
//		Bitmap picture = BitmapHelper.GetBitmapByUri(pic_utl);
		Drawable picture_draw = BitmapDrawable.createFromPath(pic_utl) ;
		Log.d(tag,"picture get"+pic_utl);
		return picture_draw;
	}

	public void take_photo(View view){
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
		SelectPictureActivity.this.startActivityForResult(intent,
				this.TakephotoCode);
		
	}
	
	public void choose_photo(View view){
		Intent intent = new Intent(Intent.ACTION_PICK,
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		
		SelectPictureActivity.this.startActivityForResult(intent, 
				this.ChoosePhotoCode);
	}
	
	public void photo_cancel(View view){
		finish();
	}
	
	@Override
	public void onActivityResult(int requestCode ,int resultCode , Intent data ){
		if (requestCode == this.TakephotoCode && 
				resultCode == SelectPictureActivity.RESULT_OK &&
				data != null){
			Bundle select = data.getExtras();
			
			if (select != null){
				Log.d("I_GET","select is : "+select.toString());
			}else {
				Log.d("I_GET","no select found ");
			}
			intentCallback.putExtra("data",select);
			setResult(1,intentCallback);
			finish();
		}else if (requestCode == this.ChoosePhotoCode &&
				resultCode == SelectPictureActivity.RESULT_OK &&
				data != null){
			Uri select = data.getData();
			ContentValues values = new ContentValues();
			values.put(Personal_Info_database.PERSON_PICTURE, select.getPath());
			new Personal_Info_database(context_this_activity).update(values, uid);
			intentCallback.setData(select);
			setResult(1,intentCallback);
			finish();
		}else {
			finish();
		}
	}
	
}
